package com.jojo.bubble.controller;

import com.jojo.bubble.entity.*;
import com.jojo.bubble.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author JoJo
 * @Date 2023/3/21 10:07
 * @Description
 * @Version 1.0
 */
@Controller
public class PageController {
    /**
     * @return java.lang.String
     * @desc 登录
     * @Date 2023/3/23 15:10
     * @Param []
     **/

    @Resource
    AgentService agentService;
    @Resource
    ShopService shopService;

    @Resource
    UserService userService;

    @Resource
    DevService devService;

    @Resource
    OrderService orderService;

    @GetMapping
    public String index() {
        return "login";
    }

    @GetMapping("/admin/login")
    public String login() {
        return "login";
    }


    /**
     * @return java.lang.String
     * @desc 首页
     * @Date 2023/3/23 15:10
     * @Param []
     **/
    @GetMapping("/admin/page/home")
    public String home(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return "login";
        }
        Order record = new Order();
        record.setStatus(1);
        List<Order> orders = orderService.getList(record);
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Order order : orders) {
            totalPrice = totalPrice.add(order.getMoney());
        }
        Integer agentNum = agentService.count(new Agent());
        Integer userNum = userService.count(new User());
        Integer devNum =  devService.count(new Dev());

        Calendar calendar = Calendar.getInstance();

        // 获取当月第一天的 0 点
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date firstDayOfMonth = calendar.getTime();

        // 获取当月最后一天的 23:59:59
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date lastDayOfMonth = calendar.getTime();

        record.setStartTime(firstDayOfMonth);
        record.setEndTime(lastDayOfMonth);
        List<Order> monthOrders = orderService.getList(record);
        BigDecimal monthPrice = BigDecimal.ZERO;
        for (Order order : monthOrders) {
            monthPrice = monthPrice.add(order.getMoney());
        }
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        Date startOfDay = calendar1.getTime();

        // 获取当日的24点（即第二天的0点）
        calendar1.add(Calendar.DAY_OF_MONTH, 1);
        Date endOfDay = calendar1.getTime();
        record.setStartTime(startOfDay);
        record.setEndTime(endOfDay);
        List<Order> todayOrders = orderService.getList(record);
        BigDecimal todayPrice = BigDecimal.ZERO;
        for (Order order : todayOrders) {
            todayPrice = todayPrice.add(order.getMoney());
        }
        request.setAttribute("agentNum", agentNum);
        request.setAttribute("userNum", userNum);
        request.setAttribute("orderNum", orders.size());
        request.setAttribute("monthNum", monthOrders.size());
        request.setAttribute("todayNum", todayOrders.size());
        request.setAttribute("devNum", devNum);
        request.setAttribute("monthPrice", monthPrice);
        request.setAttribute("totalPrice", totalPrice);
        request.setAttribute("todayPrice", todayPrice);
        return "index/home";
    }
    @GetMapping("/admin/page/agent")
    public String agent(HttpServletRequest request) {
        List<Agent> agentList = agentService.getList(new Agent());
        request.setAttribute("agentList",agentList);
        return "index/agent";
    }

    @GetMapping("/admin/page/balance")
    public String balance() {
        return "index/balance";
    }
    @GetMapping("/admin/page/shop")
    public String shop() {
        return "index/shop";
    }
    @GetMapping("/admin/page/gear")
    public String gear() {
        return "index/gear";
    }

    @GetMapping("/admin/page/role")
    public String role() {
        return "index/role";
    }

    @GetMapping("/admin/page/logs")
    public String logs() {
        return "index/logs";
    }

    @GetMapping("/admin/page/user")
    public String user() {
        return "index/user";
    }

    @GetMapping("/admin/page/order")
    public String order() {
        return "index/order";
    }

    @GetMapping("/admin/page/dev")
    public String dev(HttpServletRequest request) {
        List<Agent> agentList = agentService.getList(new Agent());
        request.setAttribute("agentList",agentList);
        return "index/dev";
    }

}
